--
-- Author: ping.wang
-- Date: 2019-05-15 18:14:50
--
-- DailyTimelineBlockRender
--

local ClassRef = app.mvc.createRenderClass(...)
local BgColorTable = {
    cc.c3b(255,168,0),
    cc.c3b(255,221,103),
    cc.c3b(249,160,137),
    cc.c3b(143,230,255),
}
local TextColorTable = {
    cc.c3b(255,225,132),
    cc.c3b(255,168,0),
}
--data =
function ClassRef.createWithData(data)
    local resStr = "ui2/activity/schedule_time_%d.csb"
    local sprite = ClassRef.extend(cc.CSLoader:createNode(string.format(resStr, data.belongLength)))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._ui.Panel:setSwallowTouches( false )
    self._isMoving = false
    self._isClose = false
    self._ui.tips:setVisible(false)
    --这里最好判断一下移动的误差 最好能取到scrollview移动了多少距离 根据距离算误差决定是否算移动
    self._ui.Panel:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            self.beginPosX = self._data.scrollView:getInnerContainer():getPosition()
            self._isMoving = false
            local point = self._data.scrollView:convertToNodeSpace(sender:getTouchBeganPosition())
            local contentSize = self._data.scrollView:getContentSize()
            self._isInScrollView = false
            --判断点击区域是否在scrollView内
            if point.x >= 0 and point.x <= contentSize.width and point.y >= 0 and point.y <= contentSize.height then
                self._isInScrollView = true
            end

        elseif eventType == ccui.TouchEventType.moved then
            if self._isInScrollView then
                local posX = self._data.scrollView:getInnerContainer():getPosition()
                if not self._isMoving and math.abs(self.beginPosX - posX) > 10 then
                    self._isMoving = true
                end
            end
        elseif eventType == ccui.TouchEventType.canceled or
                eventType == ccui.TouchEventType.ended then
            if self._isInScrollView and not self._isClose and not self._isMoving then
                local p = string.split(GD:getConvertResource(self._data.conf.resourceID), "|")
                display.resourceGotoFeature({feature=p[1], param={p[3]}})
            end
        end
    end)

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        self._isClose = false
        local data = self._data.conf
        self._ui.Image:loadTexture(string.format("art/timeline/%s.png",data.icon))
        self._ui.tlt:setString(L(data.name))

        local needReOrderNodes = {}
        needReOrderNodes[1] = self._ui["Panel_star/tlt"]

        for i = 1, 5 do
            local isVisible = checknumber(data.star) >= i
            local starNode = self._ui["Panel_star/s"..i]
            starNode:setVisible(isVisible)
            if isVisible then
                table.insert(needReOrderNodes,starNode)
            end
        end

        self:setNodesMiddle(self._ui["Panel_star"],0,needReOrderNodes)

        self._ui.bg:setColor(BgColorTable[checknumber(data.color)])
        self._ui.tlt:setTextColor(TextColorTable[checknumber(data.nameColor)])
        local isInRange = false
        local needMaskGray = true
        if self._data.curTimeRangeIdx > 0 then
            local belongArray = string.split(data.belong,";")
            for k,v in ipairs(belongArray) do
                if tonumber(v) >= self._data.curTimeRangeIdx then
                    needMaskGray = false
                    if tonumber(v) == self._data.curTimeRangeIdx then
                        isInRange = true
                    end
                    break
                end
            end

            self._ui.mask:setVisible(needMaskGray)
        else
            --不处于时间段内 可能有已经超时或者未达到时间
            local belongArray = string.split(data.belong,";")
            for k,v in ipairs(belongArray) do
                if tonumber(v) > self._data.timePoint then
                    needMaskGray = false
                    break
                end
            end

            self._ui.mask:setVisible(needMaskGray)
        end


        if isInRange then
            self._ui.num:setString(L("timeline3"))
            self._ui.num:setTextColor(cc.c3b(0,255,168))
        else

            if needMaskGray then
                self._isClose = true
                self._ui.num:setString(L("timeline1"))
            else
                self._ui.num:setString(L("timeline2"))
            end
            self._ui.num:setTextColor(cc.c3b(255,255,132))
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:setNodesMiddle(parentNode, xPadding, nodes)
    if not parentNode then return end
    local count = #nodes
    local totalLength = 0
    local lengths = {}

    for i = 1, count do
        local node = nodes[i]
        node:setAnchorPointX(0.5)
        lengths[i] = node:getContentSize().width * node:getScaleX()
        totalLength = totalLength + lengths[i]
    end

    local size = parentNode:getContentSize().width
    local interval = xPadding or 0 --每两个node之间的间距
    totalLength = totalLength + (count - 1) * interval

    local leftLen = 0
    for i = 1, count do
        local rightLen = totalLength / 2 - leftLen - lengths[i]
        local posX = size / 2 - rightLen - lengths[i] / 2
        nodes[i]:setPositionX(posX)

        leftLen = leftLen + lengths[i] + interval
    end

end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
